package com.macasa.panel.entity.impl;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.ForeignKey;

import com.google.common.base.Objects;
import com.macasa.enumerations.Os;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.entity.api.AbstractModel;
import com.macasa.panel.entity.api.IInterface;
import com.macasa.panel.entity.api.IServer;

@Entity
@Table(name = "server")
@SequenceGenerator(name = "Server_SEQ", sequenceName = "ServerIdSeq", initialValue = 1)
@Inheritance(strategy = InheritanceType.JOINED)
@Getter
@Setter
@ToString
public abstract class Server extends AbstractModel<Integer> implements IServer {

	/**
	 * The Constant serialVersionUID
	 */
	@Version
	private static final long	serialVersionUID	= 2392721394362519355L;

	@Id
	@Column(name = "server_id", nullable = false)
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "Server_SEQ")
	private Integer				id;

	@Column(name = "fqdn", nullable = false, unique = true)
	private String				fqdn;

	@Column(name = "ssh_log_user", nullable = false)
	private String				sshLogUser;

	@Column(name = "ssh_log_password", nullable = false)
	private String				sshLogPassword;

	@Column(name = "ssh_su_password", nullable = false)
	private String				sshSuPassword;

	@Column(name = "ssh_port", nullable = false)
	private Integer				sshPort;

	@Column(name = "check_server_status", nullable = false)
	private Boolean				checkServerStatus;

	@Enumerated(EnumType.STRING)
	@Column(name = "os", nullable = false)
	private Os					os;

	@Column(name = "key", nullable = true)
	private Integer				key;

	@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = com.macasa.panel.entity.impl.Interface.class)
	@JoinColumn(name = "fk_interface_to_dial", nullable = false)
	@ForeignKey(name = "fk_server_01")
	private IInterface			interfaceToDial;

	@Override
	public boolean equals(final Object obj) {

		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Server)) {
			return false;
		}

		final Server other = CastHelpers.cast(obj, Server.class);

		return Objects.equal(this.id, other.id) && Objects.equal(this.fqdn, other.fqdn) && Objects.equal(this.sshLogUser, other.sshLogUser)
				&& Objects.equal(this.sshLogPassword, other.sshLogPassword) && Objects.equal(this.sshSuPassword, other.sshSuPassword)
				&& Objects.equal(this.sshPort, other.sshPort) && Objects.equal(this.checkServerStatus, other.checkServerStatus)
				&& Objects.equal(this.os, other.os) && Objects.equal(this.key, other.key) && Objects.equal(this.interfaceToDial, other.interfaceToDial);
	}

	@Override
	public int hashCode() {
		return Objects.hashCode(this.id, this.fqdn, this.sshLogUser, this.sshLogPassword, this.sshSuPassword, this.sshPort, this.checkServerStatus, this.os,
				this.key, this.interfaceToDial);
	}
}
